<template>
	<ContentWrap>
		<!-- 搜索工作栏 -->
		<el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="90px">
			<!-- 
      <el-form-item label="手续费负担" prop="payFeeStyle">
        <el-select
          v-model="queryParams.payFeeStyle"
          placeholder="手续费负担"
          :clearable="true"
          class="!w-120px"
        >
          <el-option
            v-for="dict in elecPayFeeStyleDictDatas"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      -->
			<el-form-item label="" prop="keyWord">
				<el-input placeholder="请输入内容" v-model.trim="keyWord">
					<template #prepend>
						<el-select v-model="kType" placeholder="请选择" class="!w-120px">
							<el-option label="姓名" value="memberUserName" />
							<el-option label="商户编码" value="matCode" />
						</el-select>
					</template>
				</el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
				<el-button type="primary" plain @click="openForm('create')" v-hasPermi="['zwsj:own-meter-config:create']"> <Icon icon="ep:plus" /> 引入房东 </el-button>
			</el-form-item>
			<el-form-item>
				<el-alert show-icon :title="'银联支付手续费：' + channelFeeRate + '%'" type="success" :closable="false" />
			</el-form-item>
		</el-form>
	</ContentWrap>

	<!-- 列表 -->
	<ContentWrap>
		<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
			<el-table-column label="ID" align="center" prop="id" width="90" style="color: red" />
			<!-- <el-table-column label="手续费负担" align="center" prop="payFeeStyle" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ELEC_PAY_FEE_STYLE" :value="scope.row.payFeeStyle" />
        </template>
      </el-table-column> -->
			<el-table-column label="开户人员" align="left" prop="memberUserName" min-width="130">
				<template #default="scope">
					<el-button
						link
						style="margin-right: 10px; font-size: 14px; color: green"
						class="el-icon-s-order"
						v-if="scope.row.rentUserResp"
						@click="showOwnMeterDetail(scope.row.rentUserResp.id)">
						电
					</el-button>
					<el-button
						link
						style="margin-right: 10px; font-size: 14px; color: pink; margin-left: 0"
						class="el-icon-s-order"
						v-if="scope.row.rentUserResp"
						@click="showNetPayDetail(scope.row.rentUserResp.id)">
						网
					</el-button>
					<span>{{ scope.row.memberUserName }}</span>
				</template>
			</el-table-column>
			<el-table-column label="配置描述" align="center" prop="configRemark" width="210" />
			<el-table-column label="商户编号" align="center" prop="matCode" width="210" />
			<el-table-column label="商户有效期" align="center" prop="matExpiresTime" width="110">
				<template #default="scope">
					<div>{{ formatDate(scope.row.matExpiresTime, 'YYYY-MM-DD') }}</div>
				</template>
			</el-table-column>
			<el-table-column label="宽带充值" align="center" label-class-name="netPayClass">
				<el-table-column label="平台手续费" align="center" prop="platformNetPayRate" width="100" label-class-name="netPayClass">
					<template #default="scope">
						<div class="netPayClass">{{ scope.row.platformNetPayRate }}%</div>
					</template>
				</el-table-column>
			</el-table-column>
			<el-table-column label="电费充值" align="center" label-class-name="elecChargeClass">
				<el-table-column label="关联电表" align="center" width="100" label-class-name="elecChargeClass">
					<template #default="scope">
						<el-button class="elecChargeClass" style="color: green" link @click="getRelaMeters(scope.row.id)">查看</el-button>
					</template>
				</el-table-column>
				<el-table-column label="平台手续费" align="center" prop="platformElecChargeRate" label-class-name="elecChargeClass" width="100">
					<template #default="scope">
						<div class="elecChargeClass">{{ scope.row.platformElecChargeRate }}%</div>
					</template>
				</el-table-column>
				<el-table-column label="最少支付" align="center" prop="minPayElecCharge" label-class-name="elecChargeClass" width="100">
					<template #default="scope">
						<div class="elecChargeClass">{{ divide(scope.row.minPayElecCharge, 100) }}元</div>
					</template>
				</el-table-column>
				<el-table-column label="余额提示" align="center" prop="warnElecMoney" label-class-name="elecChargeClass" width="100">
					<template #default="scope">
						<div class="elecChargeClass">{{ divide(scope.row.warnElecMoney, 100) }}元</div>
					</template>
				</el-table-column>
			</el-table-column>
			<!-- <el-table-column label="最后操作IP" align="center" prop="lastOperIp" width="100" /> -->
			<el-table-column label="最后操作时间" align="center" prop="lastOperDate" width="180" :formatter="dateFormatter" />
			<el-table-column label="操作" class-name="fixed-width" fixed="right" width="160">
				<template #default="scope">
					<el-button link type="danger" @click="register(scope.row.id)" v-hasPermi="['zwsj:own-meter-config:register-union-account']">签约</el-button>
					<el-button link type="primary" @click="openForm('update', scope.row.id)" v-hasPermi="['zwsj:own-meter-config:update']">绑定</el-button>
					<el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['zwsj:own-meter-config:delete']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
	</ContentWrap>

	<!-- 添加或修改对话框 -->
	<OwnMeterConfigForm ref="formRef" @success="getList" />
	<OwnMeterDetail ref="ownMeterDetailRef" />
	<OwnNetPayDetail ref="ownNetPayDetailRef" />
	<own-meter-config-meters ref="ownMeterConfigMetersRef" />
</template>

<script lang="ts" setup>
	import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
	import { dateFormatter, formatDate } from '@/utils/formatTime'
	import { divide } from '@/utils/tools'
	import * as OwnMeterConfigApi from '@/api/zwsj/ownMeterConfig'
	import OwnMeterConfigForm from './OwnMeterConfigForm.vue'
	import OwnMeterDetail from './OwnMeterDetail.vue'
	import OwnNetPayDetail from './OwnNetPayDetail.vue'
	import OwnMeterConfigMeters from './OwnMeterConfigMeters.vue'
	import { getConfigKey } from '@/api/infra/config'

	defineOptions({ name: 'OwnMeterConfig' })

	const message = useMessage() // 消息弹窗
	const { t } = useI18n() // 国际化

	const loading = ref(true) // 列表的加载中
	const total = ref(0) // 列表的总页数
	const list = ref() // 列表的数据

	const keyWord = ref('')
	const kType = ref('memberUserName')
	const queryParams = reactive({
		pageNo: 1,
		pageSize: 10,
		memberUserId: undefined,
		payFeeStyle: 0,
		memberUserName: undefined,
		matCode: undefined,
	})

	const elecPayFeeStyleDictDatas = getIntDictOptions(DICT_TYPE.ELEC_PAY_FEE_STYLE)

	const queryFormRef = ref() // 搜索的表单

	/** 查询部门列表 */
	const getList = async () => {
		loading.value = true
		try {
			queryParams.memberUserName = undefined
			queryParams.matCode = undefined
			if ('memberUserName' == kType.value) {
				queryParams.memberUserName = keyWord.value || ''
			}
			if ('matCode' == kType.value) {
				queryParams.matCode = keyWord.value || ''
			}
			const data = await OwnMeterConfigApi.getOwnMeterConfigPage(queryParams)
			list.value = data.list
			total.value = data.total
		} finally {
			loading.value = false
		}
	}

	/** 搜索按钮操作 */
	const handleQuery = () => {
		getList()
	}

	/** 添加/修改操作 */
	const formRef = ref()
	const openForm = (type: string, id?: number) => {
		formRef.value.open(type, id)
	}

	/** 申请 */
	const register = async (id: number) => {
		let url = await OwnMeterConfigApi.registerUnionAccount(id)
		if (undefined != url && url != '') {
			window.open(url)
		}
	}

	/** 删除按钮操作 */
	const handleDelete = async (id: number) => {
		try {
			// 删除的二次确认
			await message.delConfirm()
			// 发起删除
			await OwnMeterConfigApi.deleteOwnMeterConfig(id)
			message.success(t('common.delSuccess'))
			// 刷新列表
			await getList()
		} catch {}
	}

	const ownMeterDetailRef = ref()
	const showOwnMeterDetail = async (ownMemberUserId: number) => {
		ownMeterDetailRef.value.open(ownMemberUserId)
	}

	const ownNetPayDetailRef = ref()
	const showNetPayDetail = async (ownMemberUserId: number) => {
		ownNetPayDetailRef.value.open(ownMemberUserId)
	}

	const ownMeterConfigMetersRef = ref()
	const getRelaMeters = async (matCodeId: number) => {
		ownMeterConfigMetersRef.value.open(matCodeId)
	}

	const channelFeeRate = ref()
	/** 初始化 **/
	onMounted(async () => {
		//查询银联支付手续费
		let channelFeeRateT = await getConfigKey('channel_fee_rate')
		channelFeeRate.value = channelFeeRateT
		await getList()
	})
</script>

<style>
	.netPayClass {
		color: red;
	}

	.elecChargeClass {
		color: green;
	}
</style>
